<template>
  <div class="evaluateContainer">
    <div class="sendMessage">
      <div class="inputtText">
        <el-input
          type="textarea"
          autosize
          placeholder="请输入内容"
          v-model="textareaVal"
        />
      </div>

      <el-button type="primary" size="small" @click="sendMessage"
        >发送</el-button
      >
    </div>
    <div class="messageList">
      <div
        class="messageLi"
        v-for="item in messageData.slice((curpage - 1) * 5, curpage * 5)"
        :key="item.commitid"
      >
        <div class="item-left">
          <div>{{ item.message }}</div>
          <div class="sendTime">{{ item.sendTime }}</div>
        </div>
        <div class="item-right">{{ item.senderName }}</div>
      </div>
    </div>
    <div class="pagination" v-if="messageData.length > 5">
      <el-pagination
        small
        layout="prev, pager, next"
        :page-size="5"
        @current-change="paginationChage"
        :total="messageData.length"
        :current-page.sync="curpage"
      />
    </div>
  </div>
</template>

<script>
import { getNowTime } from "@/utils/func.js";
let commitid = 0;
export default {
  data() {
    return {
      textareaVal: "",
      messageData: [],
      curpage: 1,
    };
  },
  computed: {
    username() {
      return sessionStorage.getItem("username");
    },
  },
  methods: {
    sendMessage() {
      if (!this.textareaVal) {
        this.$message.error("评论消息不能为空");
        return;
      }
      let sendTime = getNowTime();
      let message = this.textareaVal;
      // 登入配合seesionStroage
      let senderName = this.username;
      let gid = 1;
      let sendObj = {
        commitid: ++commitid,
        gid,
        senderName,
        sendTime,
        message,
      };
      // 前端操作
      this.messageData.unshift(sendObj);
      // 后端插入数据
    },
    paginationChage(page) {
      console.log(page);
    },
  },
};
</script>

<style lang="less" scoped>
.evaluateContainer {
  min-height: 200px;
  height: auto;
  .messageList {
    .messageLi {
      margin-top: 20px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      border-bottom: 1px solid #dbdbdb;
      padding-bottom: 2px;
      .item-left {
        .sendTime {
          margin-top: 50px;
          color: #666;
          font-size: 12px;
        }
      }
      .item-right {
      }
    }
  }
  .sendMessage {
    display: flex;
    justify-content: space-around;
    .inputtText {
      width: 80%;
    }
  }
  .pagination {
    float: right;
    margin-top: 10px;
  }
}
</style>